package com.eqdd.yiqidian.ui;

import android.databinding.DataBindingUtil;
import android.view.View;

import com.eqdd.inputs.AndroidNextInputs;
import com.eqdd.inputs.LazyLoaders;
import com.eqdd.inputs.WidgetAccess;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.nextinputs.ValueScheme;
import com.eqdd.yiqidian.EmailPasswordGetActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.BaseBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpPresneter;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.GsonUtils;
import com.eqdd.yiqidian.utils.ToastUtil;

public class EmailPasswordGetActivity extends BaseActivity {
    private EmailPasswordGetActivityCustom dataBinding;
    private AndroidNextInputs inputs;
    private WidgetAccess access;
    private String phone;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_email_password_get);
        initTopTitleBar(View.VISIBLE, "邮箱找回密码");
    }

    @Override
    public void initData() {
        phone = getIntent().getStringExtra("phone");
        inputs = new AndroidNextInputs();
        access = new WidgetAccess(this);
        // 必选，与邮件相同
        LazyLoaders loader = new LazyLoaders(this);
        inputs  // 必选，手机号
                .add(access.findEditText(R.id.et_check))
                .with(StaticScheme.Required(), ValueScheme.RangeLength(4, 4))
                // 必选 ，密码8~18
                .add(access.findEditText(R.id.et_password))
                .with(StaticScheme.Required(), ValueScheme.RangeLength(8, 18))// 必选 ，密码8~18
                .add(access.findEditText(R.id.et_password_again))
                .with(StaticScheme.Required(), ValueScheme.EqualsTo(loader.fromEditText(R.id.et_password)));
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_submit:
                if (inputs.test()){
                    new HttpPresneter.Builder<String>(HttpConfig.USER_EMAIL_GET_PASSWORD,String.class,this)
                            .Params("aname",phone)
                            .Params("ran",dataBinding.etCheck.getText().toString().trim())
                            .Params("apass", EncryptUtil.getMD5(dataBinding.etPassword.getText().toString().trim()+"EQD"))
                            .setOnHttpListener(new HttpPresneter.OnHttpListener<String>() {
                                @Override
                                public void onError(Throwable e) {

                                }
                                @Override
                                public void onNext(String o) {

                                    BaseBean baseBean = GsonUtils.changeGsonToBean(o, BaseBean.class);
                                    ToastUtil.showShort(baseBean.getMsg());
                                    if (baseBean.getStatus()==200){
                                        finish();
                                    }
                                }
                            })
                            .build()
                            .post();
                }
                break;
        }
    }
}
